package Dungeoneering;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import org.powerbot.concurrent.Task;
import org.powerbot.concurrent.strategy.Strategy;
import org.powerbot.game.api.ActiveScript;
import org.powerbot.game.api.Manifest;
import org.powerbot.game.api.methods.Calculations;
import org.powerbot.game.api.methods.input.Mouse;
import org.powerbot.game.api.methods.interactive.NPCs;
import org.powerbot.game.api.methods.interactive.Players;
import org.powerbot.game.api.methods.node.GroundItems;
import org.powerbot.game.api.methods.node.Menu;
import org.powerbot.game.api.methods.widget.Camera;
import org.powerbot.game.api.util.Filter;
import org.powerbot.game.api.util.Random;
import org.powerbot.game.api.wrappers.Tile;
import org.powerbot.game.api.wrappers.interactive.NPC;
import org.powerbot.game.api.wrappers.node.GroundItem;
import org.powerbot.game.api.wrappers.node.SceneObject;
import org.powerbot.game.bot.event.listener.PaintListener;

import Dungeoneering.ActionHandlers.ActionHandler;
import Dungeoneering.Entities.Room;
import Dungeoneering.Utils.CameraUtils;
import Dungeoneering.Utils.Utilities;
import Dungeoneering.api.methods.Objects;

/**
 * a Test script to test methods and puzzles
 * @author Nicolaas
 *
 */
@Manifest(
		name = "Dungeoneering Test script",
		authors = {"Banti", "Joske"},
		version = 1.0,
		description = "This is to test our shit",
		premium = true			
		)
public class TestScript extends ActiveScript implements PaintListener, MouseListener, MouseMotionListener {
	
	private ArrayList<Room> r = new ArrayList<Room>();
	private NPC smug;

	@Override
	protected void setup() {
		smug = NPCs.getNearest(11226);
		Tile t = Players.getLocal().getLocation();
		r.add(new Room(1, 1, t.getX(), t.getY()));
		LoopStrat loop = new LoopStrat();
		loop.setTask(loop);
		provide(loop); 
		SceneObject[] lo = Objects.getLoaded();
	}
	
	@Override
	public void onStop() {
	}
	
	private final class LoopStrat extends Strategy implements Task {
    	@Override
    	public boolean validate() {
    		return true;
    	}

 	  	@Override
  	 	public void run() {
 			GroundItem item = GroundItems.getNearest(18161);
 			NPC n = NPCs.getNearest(11226);
 			ActionHandler.interactNpc(n, "Talk-to");
 			System.out.println("jiojdoigjiogj");
 			//Utilities.sleep(5000);
 	  /*		
 	  		if (mt == null || !mt.isAlive()) {
 	  			System.out.println("starting mt");
 	  			mt = new MouseThread();
 	  			mt.start();
 	  		}
 	  		if (at == null || !at.isAlive()) {
 	  			at = new AngleThread();
 	  			at.start();
 	  		}
 	  		if (pt == null || !pt.isAlive()) {
 	  			pt = new PitchThread();
 	  			pt.start();
 	  		} */
 	  	}
    }

	@Override
	public void onRepaint(Graphics g) {
		try {
		/*if (r != null) {
				for (Room room : r) {
					Tile[] tiles = room.getArea().getTileArray();
					g.setColor(Color.YELLOW);
					for (Tile tile : tiles) {
						highlightMiniMapTile(g, tile.getX(), tile.getY(), new Color(255, 0, 0, 80));
						Polygon[] p = tile.getBounds();
						g.setColor(Color.YELLOW);
						if (p.length > 0)
							((Graphics2D) g).draw(tile.getBounds()[0]);
					}
					highlightMiniMapTile(g, tiles[0].getX(), tiles[0].getY(), new Color(255, 0, 0, 255));
				}
			}  
			if (l != null) {
				for (Location lo : l) {
					Area a = lo.getArea();
					for (int i = 0; i < a.npoints; i++) {
						highlightMiniMapTile(g, a.xpoints[i], a.ypoints[i], Color.BLUE);
					}
				}
			}*/
			highlightMiniMapTile(g, Players.getLocal().getLocation().getX(), Players.getLocal().getLocation().getY(), Color.BLUE);
			Tile ti = Players.getLocal().getLocation();
			ti.getBounds();
			g.setColor(Color.RED);
			((Graphics2D) g).draw(ti.getBounds()[0]);
			//System.out.println(Camera.getMobileAngle(smug));
		//	System.out.println(smug.isOnScreen());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void highlightMiniMapTile(Graphics g, int tileX, int tileY, Color color) {
		Point miniTL = Calculations.worldToMap((double)tileX - 0.7, (double)tileY - 0.7);
		Point miniTR = Calculations.worldToMap((double)tileX + 0.3, (double)tileY - 0.7);
		Point miniBL = Calculations.worldToMap((double)tileX - 0.7, (double)tileY + 0.3);
		Point miniBR = Calculations.worldToMap((double)tileX + 0.3, (double)tileY + 0.3);
		if ((miniTL.x == -1) || (miniTL.y == -1) || (miniTR.x == -1) || (miniTR.y == -1) || (miniBL.x == -1) || (miniBL.y == -1) || (miniBR.x == -1) || (miniBR.y == -1))
			return;
		
		g.setColor(color);
		g.fillPolygon(new int[]{miniBL.x, miniBR.x, miniTR.x, miniTL.x}, new int[]{miniBL.y, miniBR.y, miniTR.y, miniTL.y}, 4);
		g.setColor(Color.BLACK);
		g.drawPolygon(new int[]{miniBL.x, miniBR.x, miniTR.x, miniTL.x}, new int[]{miniBL.y, miniBR.y, miniTR.y, miniTL.y}, 4);
		
	}

@Override
public void mouseClicked(MouseEvent e) {
	System.out.println("we clickeeeeed");
	
}

@Override
public void mouseEntered(MouseEvent e) {
	// TODO Auto-generated method stub
	
}

@Override
public void mouseExited(MouseEvent e) {
	// TODO Auto-generated method stub
	
}

@Override
public void mousePressed(MouseEvent e) {
	System.out.println("we clickeeeeed2");
	
}

@Override
public void mouseReleased(MouseEvent e) {
	System.out.println("we clickeeeeed3");
	
}

@Override
public void mouseDragged(MouseEvent e) {
	// TODO Auto-generated method stub
	
}

@Override
public void mouseMoved(MouseEvent e) {
	//System.out.println("in smuggler: " + Npcs.getNearest(11226).contains(e.getPoint()));
	
}

}
